<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<form class="cmxform" id="parkAssort" method="post"
	action="house/assort/addPark">
	<div style="padding:0px">

		<div class="panel panel-default">
			<div class="panel-heading">
				<h5 class="panel-title">基本信息</h5>
			</div>
			<div class="panel-body">
				<p style="padding-left:10px">
					<span class="style_btn">地上车位</span> <span class="style_input"><input
						id="" name="parkNum" disabled="disabled" /> 个</span>
				</p>
				<div class="style_input">
					<!-- 平面车位-->
					<p>
						<span class="style_btn">平面车位</span> <span class="style_input"><input
							id="" name="parkPlaneNum" onkeyup="changeParkNum()" /> 个</span>
					</p>
					<div style="border:1px solid gray;padding:10px;margin:10px 0px;">
						<table id="plane" cellpadding="10" cellspacing="5" class="mytable" >
							<!-- <tr>
								<td width="90"><span>销售</span></td>
								<td colspan="3"><input id="" name="marketNum" value="" /> 个<input type="hidden" name="id"   id="entityId_ppnIds"/>
								</td>

							</tr> -->
							<tr>
								<td width="40" class="td-right"><span>租赁</span></td>
								<td width="260"><input id="" name="leaseNum" value="" /> 个<input
									type="hidden" name="id" id="entityId_ppnIds" /></td>
								<td width="60" class="td-right"><span>单价</span></td>
								<td colspan="3"><input id="" name="leasePrice" value="" /> 
								<select name="priceType" style="width:70px">
										<option value="0">元/月</option>
										<option value="1">元/年</option>
								</select></td>
							</tr>
							<tr>
								<td rowspan="2" class="td-right"><span>临停</span></td>
								<td rowspan="2"><input id="" name="tempStopNum" value="" /> 个</td>
								<td class="td-right"><span>日范围</span></td>
								<td><input id="" name="tempStopRange" value="" /></td>
								<td width="60" class="td-right"><span>日单价</span></td>
								<td><input id="" name="tempStopPrice" value="" /> 元/时</td>
							</tr>
							<tr>
								
								<td class="td-right"><span>夜范围</span></td>
								<td><input id="" name="tempStopRangeNight" value="" /></td>
								<td class="td-right"><span>夜单价</span></td>
								<td><input id="" name="tempStopPriceNight" value="" /> 元/时</td>
							</tr>
						</table>
					</div>
					<!-- 立体车位-->
					<p>
						<span class="style_btn">立体车位</span> <span class="style_input"><input
							id="" name="parkSolidNum" onkeyup="changeParkNum()" /> 个</span>
					</p>
					<div style="border:1px solid gray;padding:10px;">
						<table id="solid" cellpadding="10" cellspacing="5" class="mytable" >
							<tr>
								<td width="40" class="td-right"><span>销售</span></td>
								<td colspan="5"><input id="" name="marketNum" value="" />
									个<input type="hidden" name="id" id="entityId_psnIds" /></td>

							</tr>
							<tr>
								<td width="40" class="td-right"><span>租赁</span></td>
								<td width="260"><input id="" name="leaseNum" value="" /> 个
								</td>
								<td width="60" class="td-right"><span>单价</span></td>
								<td colspan="3"><input id="" name="leasePrice" value="" /> <select
									name="priceType"  style="width:70px">
										<option value="0">元/月</option>
										<option value="1">元/年</option>
								</select></td>
							</tr>
							<tr>
								<td rowspan="2" class="td-right"><span>临停</span></td>
								<td rowspan="2"><input id="" name="tempStopNum" value="" /> 个</td>
								<td class="td-right"><span>日范围</span></td>
								<td><input id="" name="tempStopRange" value="" /></td>
								<td width="60" class="td-right"><span>日单价</span></td>
								<td><input id="" name="tempStopPrice" value="" /> 元/时</td>
							</tr>
							<tr>
								
								<td class="td-right"><span>夜范围</span></td>
								<td><input id="" name="tempStopRangeNight" value="" /></td>
								<td class="td-right"><span>夜单价</span></td>
								<td><input id="" name="tempStopPriceNight" value="" /> 元/时</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div style="width:100%;text-align:center; padding-top:20px">
			<div id="btn_div1">
				<button class="btn btn-danger radius my-btn"
					onclick="saveParkAssort(0)" type="button">提交</button>
				<button class="btn btn-danger radius my-btn"
					onclick="saveParkAssort(1)" type="button">保 存</button>
				<a class="btn btn-default radius"
					onClick="resetForms('parkAssort');"> 重置 </a> <br />
			</div>
			<div id="btn_div2" style="display:none;">
				<shiro:hasPermission name="permission:edit">
					<button id="modifyBtn" type="button"
						class="btn btn-danger radius my-btn" onclick="saveParkAssort(3)">启用编辑</button>
					<button class="btn btn-danger radius my-btn hidbtn"
						onclick="saveParkAssort(2)" type="button">保存修改</button>
					<a class="btn btn-default radius hidbtn"
						onClick="resetForms('parkAssort');">重置</a>
				</shiro:hasPermission>
			</div>
		</div>

		<input type="hidden" name="parkSite" id="entityId_parkSite" value="0" />
		<input type="hidden" name="parkId" id="entityId_parkId" />
		<!-- 0地上 1地下 -->
	</div>
</form>

<script>
	$(function() {
		mySubmit("parkAssort", initPark, successPark);
		getParkAssortMsg();
		$("select[name='priceType']").select2();
	});
	//更改车位总数
	function changeParkNum() {
		//获取平面车位数量和立体车位数量相加后赋值为总车位数量
		var parkPlaneNum = $("#parkAssort").find("input[name='parkPlaneNum']").val();
		var parkSolidNum = $("#parkAssort").find("input[name='parkSolidNum']").val();
		if (parkPlaneNum == "" || !testNum(parkPlaneNum)) {
			parkPlaneNum = 0;
		}
		if (parkSolidNum == "" || !testNum(parkSolidNum)) {
			parkSolidNum = 0;
		}
		var parkNum = parseInt(parkPlaneNum) + parseInt(parkSolidNum);
		$("#parkAssort").find("input[name='parkNum']").val(parkNum);
	}
	//获取编辑页面数据
	function getParkAssortMsg() {
		var fkProjectid = parent.parent.parent.projectID;
		var parkSite = $("#parkAssort input[name='parkSite']").val();
		var url = "house/assort/getParkAssortByBuild";
		$.getJSON(url, {
			fkProjectId : fkProjectid,
			parkSite : parkSite
		}, function(data, status) {
			console.log(data);
			if (data.length > 0) {
				var va = data[0];
				$("#parkAssort").find("input[name='parkId']").val(va.parkId);
				$("#parkAssort").find("input[name='parkNum']").val(va.parkNum);
				$("#parkAssort").find("input[name='parkPlaneNum']").val(va.planeNum);
				$("#parkAssort").find("input[name='parkSolidNum']").val(va.solidNum);
				//$("#parkAssort").find("#plane input[name='marketNum']").val(va.pMarketNum);
				$("#parkAssort").find("#plane input[name='leaseNum']").val(va.pLeaseNum);
				$("#parkAssort").find("#plane input[name='leasePrice']").val(returnFloat(va.pLeasePrice));
				$("#parkAssort").find("#plane input[name='tempStopNum']").val(va.pTempNum);
				$("#parkAssort").find("#plane input[name='tempStopPrice']").val(returnFloat(va.pTempPrice));
				$("#parkAssort").find("#plane input[name='tempStopRange']").val(va.pTempStopRange);
				$("#parkAssort").find("#plane input[name='id']").val(va.pId);
				$("#parkAssort").find("#plane input[name='tempStopPriceNight']").val(returnFloat(va.nightPrice));
				$("#parkAssort").find("#plane input[name='tempStopRangeNight']").val(va.pTempStopRangeNight);
				$("#parkAssort").find("#plane select[name='priceType']").val(va.ppriceType).trigger("change");
				$("#parkAssort").find("#solid input[name='marketNum']").val(va.sMarketNum);
				$("#parkAssort").find("#solid input[name='leaseNum']").val(va.sLeaseNum);
				$("#parkAssort").find("#solid input[name='leasePrice']").val(returnFloat(va.sLeasePrice));
				$("#parkAssort").find("#solid input[name='tempStopNum']").val(va.sTempNum);
				$("#parkAssort").find("#solid input[name='tempStopPrice']").val(returnFloat(va.sTempPrice));
				$("#parkAssort").find("#solid input[name='tempStopRange']").val(va.sTempStopRange);
				$("#parkAssort").find("#solid input[name='id']").val(va.sId);
				$("#parkAssort").find("#solid input[name='tempStopPriceNight']").val(returnFloat(va.solidNightPrice));
				$("#parkAssort").find("#solid input[name='tempStopRangeNight']").val(va.sTempStopRangeNight);
				$("#parkAssort").find("#solid select[name='priceType']").val(va.spriceType).trigger("change");
				$("#parkAssort").find("#btn_div2").show();
				$("#parkAssort").find("#btn_div1").hide();
				$("#parkAssort").find("input,textarea").attr("readonly", true);
				$("#parkAssort").find("select").attr("disabled", true);
			} else {
				$("#parkAssort").find("#btn_div2").hide();
				$("#parkAssort").find("#btn_div1").show();
			}
		});
	}
	var optParkAssort;
	//optParkAssort操作字段，0：临时保存；1：新增；2：修改
	function saveParkAssort(val) {
		optParkAssort = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#parkAssort").find("input,textarea").attr("readonly", false);
				$("#parkAssort").find("#modifyBtn").hide();
				// $(".hidbtn").removeClass("hidbtn");
				$("#parkAssort").find(".hidbtn").addClass("showbtn");
				$("#parkAssort").find("select").attr("disabled", false);
				layer.close(index);
			});
		} else {

			$("#parkAssort").submit();
		}
	}
	var x; //load弹窗index
	//封装表单数据为json
	function initPark() {
		x = layer.load(2);
		//封装基础数据
		var fkProjectid = parent.parent.parent.projectID;
		var parkSite = $("#parkAssort input[name='parkSite']").val();
		var parkNum = $("#parkAssort input[name='parkNum']").val();
		var parkPlaneNum = $("#parkAssort input[name='parkPlaneNum']").val();
		var parkSolidNum = $("#parkAssort input[name='parkSolidNum']").val();
		var parkId = $("#parkAssort").find("input[name='parkId']").val();
		//封装平面数据
		var $t = $("#plane");
		var planeNumSum = 0; //平面车位总数
		var plane = new Array();
		for (var i = 0; i < $t.length; i++) {
			var v = $t[i];
			var sId = $(v).find("input[name='id']").val();
			//var marketNum = $(v).find("input[name='marketNum']").val();
			var leaseNum = $(v).find("input[name='leaseNum']").val();
			var leasePrice = $(v).find("input[name='leasePrice']").val();
			var tempStopNum = $(v).find("input[name='tempStopNum']").val();
			var tempStopPrice = $(v).find("input[name='tempStopPrice']").val();
			var priceType = $(v).find("select[name='priceType']").val();
			var tempStopPriceNight = $(v).find("input[name='tempStopPriceNight']").val();
			var tempStopRange = $(v).find("input[name='tempStopRange']").val();
			var tempStopRangeNight = $(v).find("input[name='tempStopRangeNight']").val();
			if (!testNum(leaseNum)) {
				layer.msg("平面车位中租赁个数请输入数值");
				$(v).find("input[name='leaseNum']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(leasePrice)) {
				layer.msg("平面车位中租赁单价请输入数值");
				$(v).find("input[name='leasePrice']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(tempStopNum)) {
				layer.msg("平面车位中临停个数请输入数值");
				$(v).find("input[name='tempStopNum']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(tempStopPrice)) {
				layer.msg("平面车位中临停日单价请输入数值");
				$(v).find("input[name='tempStopPrice']").focus();
				layer.close(x);
				return null;
			}else if (!testNum(tempStopPriceNight)) {
				layer.msg("平面车位中临停夜单价请输入数值");
				$(v).find("input[name='tempStopPriceNight']").focus();
				layer.close(x);
				return null;
			}
			planeNumSum = parseInt(leaseNum) + parseInt(tempStopNum); //平面车位总数=销售车位数+租赁车位数+临停车位数
			plane.push({
				id : sId,
				marketNum : 0,
				leaseNum : leaseNum,
				leasePrice : leasePrice,
				tempStopNum : tempStopNum,
				tempStopPrice : tempStopPrice,
				fkProjectid : fkProjectid,
				priceType:priceType,
				tempStopPriceNight:tempStopPriceNight,
				tempStopRangeNight:tempStopRangeNight,
				tempStopRange:tempStopRange
			});

		}


		//封装立体数据
		var solid = new Array();
		var pId = $("#solid").find("input[name='id']").val();
		var solidNumSum = 0; //立体车位数
		if (parkSolidNum != "" && parkSolidNum != 0) {
			var $t = $("#solid");

			for (var i = 0; i < $t.length; i++) {
				var v = $t[i];
				pId = $(v).find("input[name='id']").val();
				var marketNum = $(v).find("input[name='marketNum']").val();
				var leaseNum = $(v).find("input[name='leaseNum']").val();
				var leasePrice = $(v).find("input[name='leasePrice']").val();
				var tempStopNum = $(v).find("input[name='tempStopNum']").val();
				var tempStopPrice = $(v).find("input[name='tempStopPrice']").val();
				var priceType = $(v).find("select[name='priceType']").val();
				var tempStopPriceNight2 = $(v).find("input[name='tempStopPriceNight']").val();
				var tempStopRange = $(v).find("input[name='tempStopRange']").val();
				var tempStopRangeNight = $(v).find("input[name='tempStopRangeNight']").val();
				if (!testNum(marketNum)) {
					layer.msg("立体车位中销售个数请输入数值");
					$(v).find("input[name='marketNum']").focus();
					layer.close(x);
					return null;
				} else if (!testNum(leaseNum)) {
					layer.msg("立体车位中租赁个数请输入数值");
					$(v).find("input[name='leaseNum']").focus();
					layer.close(x);
					return null;
				} else if (!testNum(leasePrice)) {
					layer.msg("立体车位中租赁单价请输入数值");
					$(v).find("input[name='leasePrice']").focus();
					layer.close(x);
					return null;
				} else if (!testNum(tempStopNum)) {
					layer.msg("立体车位中临停个数请输入数值");
					$(v).find("input[name='tempStopNum']").focus();
					layer.close(x);
					return null;
				} else if (!testNum(tempStopPrice)) {
					layer.msg("立体车位中临停单价请输入数值");
					$(v).find("input[name='tempStopPrice']").focus();
					layer.close(x);
					return null;
				}else if (!testNum(tempStopPriceNight)) {
					layer.msg("立体车位中临停夜单价请输入数值");
					$(v).find("input[name='tempStopPriceNight']").focus();
					layer.close(x);
					return null;
				}
				solidNumSum = parseInt(marketNum) + parseInt(leaseNum) + parseInt(tempStopNum); //立体车位总数=销售车位数+租赁车位数+临停车位数
				solid.push({
					id : pId,
					marketNum : marketNum,
					leaseNum : leaseNum,
					leasePrice : leasePrice,
					tempStopNum : tempStopNum,
					tempStopPrice : tempStopPrice,
					priceType:priceType,
					tempStopPriceNight:tempStopPriceNight2,
					tempStopRangeNight:tempStopRangeNight,
					tempStopRange:tempStopRange
				});
			}
		} else {
			parkSolidNum = 0;
			solid.push({
				id : pId,
				marketNum : 0,
				leaseNum : 0,
				leasePrice : 0,
				tempStopNum : 0,
				tempStopPrice : 0,
				priceType:0,
				tempStopPriceNight:0
			});
		}
		if (!testNum(parkNum)) {
			layer.msg("地上车位请输入数值");
			$("#parkAssort input[name='parkNum']").focus();
			layer.close(x);
			return null;
		} else if (!testNum(parkPlaneNum)) {
			layer.msg("平面车位请输入数值");
			$("#parkAssort input[name='parkPlaneNum']").focus();
			layer.close(x);
			return null;
		} else if (!testNum(parkSolidNum)) {
			layer.msg("立体车位请输入数值");
			$("#parkAssort input[name='parkSolidNum']").focus();
			layer.close(x);
			return null;
		}
		//物业录的数据尚不完善，暂时先注释掉,后期数据完善后再添加该功能，切记勿删！！！！！！！！
		/* else if (parkPlaneNum < planeNumSum) {
			layer.msg("平面车位总数不能小于平面销售、租赁、临停车位总和");
			$("#parkAssort input[name='parkPlaneNum']").focus();
			layer.close(x);
			return null;
		}else if (parkSolidNum < solidNumSum) {
			layer.msg("立体车位总数不能小于立体销售、租赁、临停车位总和");
			$("#parkAssort input[name='parkSolidNum']").focus();
			layer.close(x);
			return null;
		} */ 
		else {
			return {
				id : parkId,
				fkProjectid : fkProjectid,
				parkSite : parkSite,
				parkNum : parkNum,
				parkPlaneNum : parkPlaneNum,
				parkSolidNum : parkSolidNum,
				parkPlaneFk2 : plane,
				parkSolidFk2 : solid,
				parkType : 0,
				opt : optParkAssort
			};
		}
	}

	//成功后调用方法
	function successPark(responseText) {
		if (responseText.success) {
			layer.alert("保存成功！", {
				offset : '20%',
				shade : [ 0.1, '#fff' ]
			}, function() {
				location.reload();
			});
			layer.close(x);

		} else {
			layer.alert('保存失败！');
			layer.close(x);
		}
	}
</script>